using Microsoft.AspNetCore.Mvc;
using star_questionnair.Models;
using star_questionnair.Services;
using System;
using System.Threading.Tasks;

namespace star_questionnair.Controllers
{
    [ApiController]
    [Route("questionnaire")]
    public class QuestionnaireController : ControllerBase
    {
        private readonly IQuestionnaireService _questionnaireService;

        public QuestionnaireController(IQuestionnaireService questionnaireService)
        {
            _questionnaireService = questionnaireService;
        }

        [HttpPost("create")]
        public async Task<IActionResult> CreateQuestionnaireAsync([FromQuery] int authorId, [FromQuery] string title, [FromQuery] string description)
        {
            await _questionnaireService.InsertQuestionnaireAsync(authorId, title, description);
            var result = await _questionnaireService.GetQuestionnaireAfterCreateAsync(authorId, title);
            return Ok(new { success = true, data = result });
        }

        [HttpGet("get/{id}")]
        public async Task<IActionResult> GetQuestionnaireByIdAsync(int id)
        {
            var questionnaire = await _questionnaireService.GetQuestionnaireByIdAsync(id);
            if (questionnaire != null)
            {
                return Ok(new { success = true, data = questionnaire });
            }
            return NotFound(new { success = false, message = "Not Found Questionnaire" });
        }

        [HttpPut("update")]
        public async Task<IActionResult> UpdateQuestionnaireAsync([FromBody] Questionnaire questionnaire)
        {
            try
            {
                await _questionnaireService.UpdateQuestionnaireAsync(questionnaire);
                return Ok(new { success = true, message = "Questionnaire updated successfully" });
            }
            catch (Exception)
            {
                return BadRequest(new { success = false, message = "Update failed" });
            }
        }

        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeleteQuestionnaireAsync(int id)
        {
            await _questionnaireService.DeleteQuestionnaireAsync(id);
            return Ok(new { success = true, message = "Questionnaire deleted successfully" });
        }

        [HttpPut("updateDeadline")]
        public async Task<IActionResult> UpdateDeadlineAsync([FromQuery] int questionnaireId, [FromQuery] DateTime newDeadline)
        {
            try
            {
                await _questionnaireService.UpdateDeadlineAsync(questionnaireId, newDeadline);
                return Ok(new { success = true, message = "Deadline updated successfully" });
            }
            catch (Exception)
            {
                return BadRequest(new { success = false, message = "Failed update" });
            }
        }

        [HttpPut("updateStatus/{questionnaireId}")]
        public async Task<IActionResult> UpdateQuestionnaireStatusAsync(int questionnaireId)
        {
            try
            {
                await _questionnaireService.UpdateQuestionnaireStatusAsync(questionnaireId);
                return Ok(new { success = true, message = "Status updated successfully" });
            }
            catch (Exception)
            {
                return BadRequest(new { success = false, message = "Failed to update status" });
            }
        }

        [HttpGet("responseCount/{questionnaireId}")]
        public async Task<IActionResult> GetQuestionnaireResponseCountAsync(int questionnaireId)
        {
            int count = await _questionnaireService.GetQuestionnaireResponseCountAsync(questionnaireId);
            return Ok(new { success = true, data = count });
        }

        [HttpPost("submitWholeQuestionnaire")]
        public async Task<IActionResult> SubmitWholeQuestionnaireAsync([FromBody] Questionnaire questionnaire)
        {
            var result = await _questionnaireService.SubmitWholeQuestionnaireAsync(questionnaire);
            return Ok(new { success = true, data = result });
        }

        [HttpPut("updateWholeQuestionnaire")]
        public async Task<IActionResult> UpdateWholeQuestionnaireAsync([FromBody] Questionnaire questionnaire)
        {
            var result = await _questionnaireService.UpdateWholeQuestionnaireAsync(questionnaire);
            return Ok(new { success = true, data = result });
        }

        [HttpDelete("deleteWholeQuestionnaire/{questionnaireId}")]
        public async Task<IActionResult> DeleteWholeQuestionnaireAsync(int questionnaireId)
        {
            await _questionnaireService.DeleteWholeQuestionnaireAsync(questionnaireId);
            return Ok(new { success = true, message = "Deleted successfully, no object returned" });
        }
    }
}
